package com.goldenSubmarine.ang.rotator 
{
	import com.greensock.TweenMax;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import net.badimon.five3D.display.MovieClip3D;
	import net.badimon.five3D.display.Sprite3D;
	/**
	 * ...
	 * @author Bartłomiej JERZY
	 */
	public class Item3D extends Sprite3D
	{
		private const DEPTH					:int = 40
		
		private var _container	 			:Sprite3D
	
		private var _photo					:Photo 
		private var _front					:MovieClip3D 
		private var _left					:Sprite3D = new Sprite3D
		private var _right					:Sprite3D = new Sprite3D
		private var _top					:Sprite3D
		private var _bottom					:Sprite3D
		
		public var n						:Item3D
		public var p						:Item3D
		public var t						:Number
		public var id						:Number
		
		
		private var _data					:Object
		private var _selected 				:Boolean = false
		
		public function Item3D($data:Object) 
		{
			_data = $data
			init()
			redraw()
		}
		
		private function init():void
		{
			_left.rotationY  = 90
			_right.rotationY  = -90
			
			
			
			_photo = new Photo(_data)
			_photo.addEventListener(Event.COMPLETE,photoLoaded)
			_front = new MovieClip3D(_photo,new Rectangle(0,0,_photo.width,_photo.height))
			_front.activate()
						
			_left.x = - Photo.WIDTH/2
			_right.x =  Photo.WIDTH/2
			_right.y =  -Photo.HEIGHT/2
			_left.y =  -Photo.HEIGHT/2
			_front.x = _left.x
			_front.y = _left.y
			
			addChild(_left)
			addChild(_right)
			addChild(_front)
			
			this.addEventListener(MouseEvent.CLICK,clickHandler)
			this.buttonMode = true
		}
		
		private function photoLoaded(e:Event):void 
		{
			
			if (_photo.opened == 0 || _photo.opened == 1) {
				_front.deactivate()
				trace("zbychu",_photo.opened)
			}
		}
		
		private function clickHandler(e:MouseEvent):void 
		{
			if (_selected) {
				navigateToURL(new URLRequest(String(_data.link)),"_self")
			}else {
				dispatchEvent(new Event(Event.SELECT))
			}
		}
		
		private function redraw():void {
			_left.graphics3D.clear()
			_right.graphics3D.clear()
			
			_left.graphics3D.beginFill(0xeeeeee)
			_left.graphics3D.drawRect(0,0,-DEPTH,_photo.height)
			_left.graphics3D.endFill()
			
			_right.graphics3D.beginFill(0xeeeeee)
			_right.graphics3D.drawRect(0,0,DEPTH,_photo.height)
			_right.graphics3D.endFill()
			if(_front.clippingRectangle.height != _photo.height) _front.clippingRectangle = new Rectangle(0,0,_photo.width,_photo.height)
		}
		
		public function destroy():void {
			
		}
		
		
		public function get selected():Boolean { return _selected; }
		
		public function set selected(value:Boolean):void 
		{
			_selected = value;
			select()
		}
		
		private function select():void
		{
			_front.activate()
			if (_selected) {
				if(_photo.loaded){
					TweenMax.to(_photo, 0.4, { opened:1, onUpdate:redraw, onComplete:_front.deactivate } )
				}else {
					TweenMax.to(_photo, 0.4, { opened:1, onUpdate:redraw, onComplete:function():void { if (_photo.loaded)_front.deactivate(); }} )
				}
			}else {
				if(_photo.loaded){
					TweenMax.to(_photo, 0.2, { opened:0, onUpdate:redraw, onComplete:_front.deactivate } )
				}else {
					TweenMax.to(_photo, 0.2, { opened:0, onUpdate:redraw, onComplete:function():void { if (_photo.loaded)_front.deactivate(); }} )
				}
			}
		}
		
	}

}